package com.rtp2p.rtcoresdkdemo.add.ble

import android.app.Application
import android.content.Context
import android.os.Handler
import android.os.Looper
import androidx.lifecycle.MutableLiveData
import com.rtp2p.rtcoresdkdemo.base.BaseViewModel
import com.rtp2p.rtp2pcore.core.BindingBleDevice
import com.rtp2p.rtp2pcore.core.BindingDevice
import com.rtp2p.rtp2pcore.core.SearchBleDevice
import com.rtp2p.rtp2pcore.utils.RTLogUtils

class AddBindingBleViewModel(application: Application) : BaseViewModel(application) {
    val msg = MutableLiveData<String>()
    var adapter: AddBindingBleAdapter? = null
        private set

    private var searchBleDevice: SearchBleDevice? = null
    private var isBinding = false

    fun init(
        context: Context, ssid: String, pwd: String, finishCallback: () -> Unit
    ) {
        adapter = AddBindingBleAdapter { bleDevice ->
            startBindingBleDevice(context, bleDevice, ssid, pwd, finishCallback)
        }
        searchBleDevice = SearchBleDevice(context) {
            stopSearch()
            if (it.success) {
                if ((adapter?.size() ?: 0) > 0) return@SearchBleDevice
                adapter?.add(it.data)
            }
        }
    }

    internal fun onStart() {
        startSearch()
    }

    internal fun onStop() {
        stopSearch()
        stopBindingBleDevice()
    }

    private fun startSearch() {
        updateUiMsg("开始搜索")
        adapter?.clear()
        searchBleDevice?.startSearch()
    }

    private fun stopSearch() {
        updateUiMsg("")
        searchBleDevice?.stopSearch()
    }

    //是否正在绑定
    private var bindingBleDevice: BindingBleDevice? = null
    private fun startBindingBleDevice(
        context: Context, bleDevice: SearchBleDevice.BleDevice,
        ssid: String, pwd: String,
        finishCallback: () -> Unit
    ) {
        if (bindingBleDevice != null) return

        updateUiMsg("开始配置网络")
        bindingBleDevice = BindingBleDevice(context, bleDevice)
        bindingBleDevice?.configData(ssid, pwd) {
            stopBindingBleDevice()
            if (it.success) {
                binding(context, it.data, finishCallback)
            } else {
                updateUiMsg("配置网络失败： code:${it.code} message:${it.message}")
            }
        }
    }

    private fun stopBindingBleDevice() {
        bindingBleDevice?.destroy()
        bindingBleDevice = null
    }

    //绑定设备
    private val bindingDevice = BindingDevice()
    private fun binding(
        context: Context,
        deviceId: String,
        finishCallback: () -> Unit
    ) {
        if (isBinding) return
        isBinding = true

        updateUiMsg("$deviceId 开始绑定")
        bindingDevice.binding(context, deviceId, "camera") {
            Handler(Looper.getMainLooper()).post {
                isBinding = false
                if (it.success) {
                    updateUiMsg("$deviceId 绑定成功")
                    finishCallback()
                } else {
                    updateUiMsg("绑定失败： code:${it.code} message:${it.message}")
                }
            }
        }
    }

    private fun updateUiMsg(msgValue: String) {
        msg.value = msgValue
        RTLogUtils.d(msgValue)
    }
}